import QtQuick 2.12
import QtQuick.Controls 2.12

ApplicationWindow {
    id: testWindow
    visible: true
    width: 400
    height: 300
    title: "测试精简JSON格式"

    // 模拟新的sendControlCommand函数逻辑
    function testNewJsonFormat(direction) {
        console.log("测试发送控制命令: " + direction)
        
        // 模拟generateRawCommands返回的完整数据
        var fullCommands = []
        
        // 转向控制 (D121)
        var turnCommand = {
            "register": "D121",
            "address": 121,
            "functionCode": 6,
            "hexCommand": "010600790000",
            "value": 0
        }
        
        // 行进控制 (D122)
        var moveCommand = {
            "register": "D122",
            "address": 122,
            "functionCode": 6,
            "hexCommand": "0106007A0019",
            "value": 25
        }
        
        // 根据方向设置数值
        if (direction === "forward") {
            moveCommand.value = 25
            moveCommand.hexCommand = "0106007A0019"
        } else if (direction === "backward") {
            moveCommand.value = -25
            moveCommand.hexCommand = "0106007AFFE7"
        } else if (direction === "left") {
            turnCommand.value = -12
            turnCommand.hexCommand = "01060079FFF4"
        } else if (direction === "right") {
            turnCommand.value = 12
            turnCommand.hexCommand = "01060079000C"
        }
        
        fullCommands.push(turnCommand)
        fullCommands.push(moveCommand)
        
        // 只提取下位机需要的三个字段：address、value、functionCode
        var rawCommands = []
        for (var i = 0; i < fullCommands.length; i++) {
            var cmd = fullCommands[i]
            rawCommands.push({
                "address": cmd.address,
                "value": cmd.value,
                "functionCode": cmd.functionCode
            })
        }
        
        // 创建精简的JSON格式，只包含下位机必需的字段
        var command = {
            "robot_sn": "ROBOT_1004",  // 下位机要求的设备SN字段
            "command": {
                "rawCommands": rawCommands       // 下位机要求的rawCommands数组在command对象内
            }
        }
        
        console.log("生成的精简JSON命令:")
        console.log(JSON.stringify(command))
        
        return command
    }

    ColumnLayout {
        anchors.centerIn: parent
        spacing: 20

        Label {
            text: "测试精简JSON格式"
            font.pixelSize: 18
            font.bold: true
            Layout.alignment: Qt.AlignHCenter
        }

        RowLayout {
            Layout.alignment: Qt.AlignHCenter
            spacing: 10

            Button {
                text: "前进"
                onClicked: testNewJsonFormat("forward")
            }
            Button {
                text: "后退"
                onClicked: testNewJsonFormat("backward")
            }
            Button {
                text: "左转"
                onClicked: testNewJsonFormat("left")
            }
            Button {
                text: "右转"
                onClicked: testNewJsonFormat("right")
            }
        }

        TextArea {
            id: resultText
            Layout.preferredWidth: 350
            Layout.preferredHeight: 150
            readOnly: true
            text: "点击按钮测试新的精简JSON格式"
        }
    }

    // 重定向console.log到文本区域
    Connections {
        target: testWindow
        onMessage: {
            resultText.text = message + "\n" + resultText.text
        }
    }
}